import Vue from "vue";
import Vuex from "vuex";
import {
  roleRoutesApi,
  ApplyStatesApi,
  AuditStatesApi,
  LeaveTypesApi,
  commodityModuleApi,
} from "@/api/apis";
import { Message } from "element-ui";
import { geneRoutesForNav, geneRoutesForRouter } from "@/utils/generateRoutes";
// import flat from "@/data/flatRoutes";
import Convert from "@/utils/TreeFlatConvert"
Vue.use(Vuex);

//单向绑定
/*A <- B <-C
10   13  18
20   23  28
22   25  30*/
//Vuex 里的数据是观察链的主控端 响应式数据的源头
let defaultUserInfo = {
  username: "anonymous007",
  userId: 0,
  nickname: "匿名swagger",
  lastLoginTime: "2021-07-01T09:45:23.88",
};
let cmTypesRoot = {
  typeId: 0,
  typeName: "虚拟根节点",
  typeParentId: -1,
  typeLevel: 1,
};
export default new Vuex.Store({
  state: {
    isAuth: false,

    userInfo: {
      username: "anonymous007",
      userId: 0,
      nickname: "匿名swagger",
      lastLoginTime: "2021-07-01T09:45:23.88",
    },
    applyStates: [
      {
        index: 0,
        text: "未提交",
        value: "saved",
      },
      {
        index: 1,
        text: "已提交",
        value: "submitted",
      },
    ],

    auditStates: [
      {
        index: 0,
        text: "未审核",
        value: "notAudit",
      },
      {
        index: 1,
        text: "审核失败",
        value: "auditFailed",
      },
      // {
      //   index: 2,
      //   text: "审核成功",
      //   value: "auditSucceed",
      // },
    ],

    leaveTypes: [
      {
        index: 0,
        text: "年假",
        value: "annualLeave",
      },
      {
        index: 1,
        text: "病假",
        value: "sickLeave",
      },
      // {
      //   index: 2,
      //   text: "事假",
      //   value: "issueLeave",
      // },
    ],
    commodityTypesTree: {},
    commodityTypesFlat:[],

    routesFromDB: [],
    routesForNav: {},
    routesForRouter: [],
  },
  mutations: {
    updateUserInfo(state, payload) {
      if (payload !== undefined) {
        state.userInfo = payload;
      }
      state.isAuth = true;
    },
    cleanAuth(state, payload) {
      state.userInfo = defaultUserInfo;
      state.isAuth = false;
      state.routesForNav = [];
      state.routesForRouter = [];
      state.routesFromDB = [];
    },

    loadDynamicRoutes(state, payload) {
      state.routesFromDB = payload;
      state.routesForNav = geneRoutesForNav(payload);
      state.routesForRouter = geneRoutesForRouter(payload);
      //  以后可以继续localStorage  sessionStorage
      // sessionStorage.setItem("routesFromDB",JSONstate.routesFromDB)
    },

    updateApplyStates(state, payload) {
      state.applyStates = payload;
    },
    updateAuditStates(state, payload) {
      state.auditStates = payload;
    },
    updateLeaveTypes(state, payload) {
      state.leaveTypes = payload;
    },
    updateCommodityTypesTree(state, payload) {
      state.commodityTypesTree = payload;
    },
    updateCommodityTypesFlat(state, payload) {
      state.commodityTypesFlat = payload;
    },
  },

  actions: {
    loadDynamicRoutes({ commit }) {
      return roleRoutesApi().then(
        (res) => {
          //假数据 后端需要开发接口
          // Message.warning(JSON.stringify(res.data));
          commit("loadDynamicRoutes", res.data);
          return res;
        },
        (error) => {
          console.log(" request error : " + error.response.status);
          throw error;
        }
      );
    },
    getApplyStates({ commit }) {
      // axios({
      //   method: "get",
      //   url: "/basic/ApplyStates",
      // })
      ApplyStatesApi().then(
        (res) => {
          commit("updateApplyStates", res.data);
        },
        (error) => {
          console.log(" request error : " + error.response.status);
        }
      );
    },
    getAuditStates({ commit }) {
      AuditStatesApi().then(
        (res) => {
          commit("updateAuditStates", res.data);
        },
        (error) => {
          console.log(" request error : " + error.response.status);
        }
      );
    },
    getLeaveTypes({ commit }) {
      LeaveTypesApi().then(
        (res) => {
          commit("updateLeaveTypes", res.data);
        },
        (error) => {
          console.log(" request error : " + error.response.status);
        }
      );
    },
    getCommodityTypesTree({ commit }) {
      commodityModuleApi.commodityTypesApi().then(
        (res) => {
          let flatNodes = JSON.parse(JSON.stringify(res.data));
          //添加根节点 添加children属性
          commit("updateCommodityTypesFlat",res.data);
          //初始化
          flatNodes.push(cmTypesRoot);
          flatNodes.forEach((x) => (x.children = []));

          let c = new Convert({
            flatNodes: flatNodes,
          });
          //添加虚拟根节点
          let p =   c.toTreeDirect("typeId", "typeParentId");
          console.log(p)
          commit("updateCommodityTypesTree",p);
        },
        (error) => {
          console.log(" request error : " + error.response.status);
        }
      );
    },
  },
  modules: {},
});
